//
//  VisitorLoginView.swift
//  SinaWeibo
//
//  Created by yang on 15/11/8.
//  Copyright © 2015年 yang. All rights reserved.
//

import UIKit

//代理协议
protocol VisitorLoginViewDelegate :NSObjectProtocol{
    
    func visitorWillLogin()
    func visitorWillRegister()
}

class VisitorLoginView: UIView {
    //代理属性
   weak var visitorLoginViewDelegate : VisitorLoginViewDelegate?
    // loginButton点击方法
    @objc func loginButtonDidClick(){
        //调用代理协议
        visitorLoginViewDelegate?.visitorWillLogin()
    }
    // registerButton点击方法
    @objc func registerButtonDidClick(){
        //调用代理协议
        visitorLoginViewDelegate?.visitorWillRegister()
        
    }
    //设置页面信息
    func setUIInfo(imageName:String?,title:String){
        iconView.hidden = false
        label.text = title
        if imageName != nil {
            circleView.image = UIImage(named: imageName!)
            bringSubviewToFront(circleView)
            iconView.hidden = true
        }else{
            
            startAnimation()
        }
    }
    //circleView 旋转
    private func startAnimation(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.repeatCount = MAXFLOAT
        anim.toValue = 2 * M_PI
        anim.duration = 20
        anim.removedOnCompletion = false
        circleView.layer.addAnimation(anim, forKey: nil)
    }
    //重写构造方法
    init() {
        super.init(frame: CGRectZero)
        setUPadtaUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //加载子控件
    private func setUPadtaUI(){
        addSubview(circleView)
        addSubview(backView)
        addSubview(iconView)
        addSubview(label)
        addSubview(loginButton)
        addSubview(registerButton)
        
        //让frame失效
        for subview in subviews{
            subview.translatesAutoresizingMaskIntoConstraints = false
        }
        //设置自动布局
        //circleView
        circleView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-60)
        }
        //iconView
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.centerY.equalTo(circleView)
        }
        //label
        label.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleView)
        make.top.equalTo(circleView.snp_bottom).offset(26)
            make.width.equalTo(224)
            make.height.equalTo(50)
        }
        //loginButton
        loginButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(label)
            make.top.equalTo(label.snp_bottom).offset(16)
            make.width.equalTo(100)
            make.height.equalTo(35)
        }
        //registerButton
        registerButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(label)
            make.top.equalTo(label.snp_bottom).offset(16)
            make.width.equalTo(100)
            make.height.equalTo(35)
        }
        //backView
        backView.snp_makeConstraints { (make) -> Void in
            make.left.right.top.equalTo(self)
            make.bottom.equalTo(registerButton.snp_top)
        }
        backgroundColor = UIColor(white: 0.93, alpha: 1)
        //添加点击事件
        loginButton.addTarget(self, action: "loginButtonDidClick", forControlEvents: .TouchUpInside)
        registerButton.addTarget(self, action: "registerButtonDidClick", forControlEvents: .TouchUpInside)
    }
    //懒加载子控件
    private lazy var circleView :UIImageView  = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var iconView :UIImageView  = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var backView :UIImageView  = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
   private lazy  var label:UILabel = UILabel(title: "关注一些人，回这里看看有什么,惊喜关注一些人，回这里看看有什么惊喜", size: 14, color: UIColor.lightGrayColor())
   private lazy var loginButton : UIButton = UIButton(title: "登陆", backImage: "common_button_white_disable", color: UIColor.lightGrayColor())
   private lazy var registerButton : UIButton = UIButton(title: "注册", backImage: "common_button_white_disable", color: UIColor.lightGrayColor())
}
